package com.thti.mecool.client.command.result.communication;

import java.util.List;

import android.os.Bundle;
import android.os.Message;

import com.threeti.mecool.mobile.interfaces.dto.CommunicationDto;
import com.thti.mecool.client.activity.CommunicationActivity;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.command.ICommand;
import com.thti.mecool.client.model.biz.CommunicationBizImpl;
import com.thti.mecool.client.model.po.Communication;
import com.thti.mecool.client.protocol.communication.CommunicationResult;
import com.thti.mecool.client.service.Protocol;

public class SearchCommunicationCommand implements ICommand{
	@Override
	public void doCommand(Object o) {
		CommunicationResult communicationResult=(CommunicationResult)o;
		if(communicationResult!=null){			
			if(communicationResult.getStatus()==1){
				System.out.println("communicationResult.getPage().getPage()>>>>>"+communicationResult.getPage().getPage());
				if((communicationResult.getPage().getPage()+1)==1){
					CommunicationBizImpl communicationBizImpl=new CommunicationBizImpl(Cache.getmContext());
					communicationBizImpl.delete();
					List<CommunicationDto> communicationDtos=communicationResult.getCommunications();
					if(communicationDtos.size()>0){
						for(CommunicationDto communicationDto:communicationDtos){
							System.out.println(communicationDto);
							Communication communication=new Communication();
							communication.setCommunicationId(communicationDto.getCommId());
							communication.setContactName(communicationDto.getContactName());
							communication.setContactPhoneOrOther(communicationDto.getContactPhoneOrOther());
							communication.setContactPosition(communicationDto.getContactPosition());
							communication.setContent(communicationDto.getContent());
							communication.setProjId(communicationDto.getProjId());
							communication.setProjStoreId(communicationDto.getProjStoreId());
							communication.setTitle(communicationDto.getTitle());
							communication.setType(communicationDto.getType());
							communication.setUserId(String.valueOf(BaseActivity.getCurrentUser().getId()));
							communication.setRecordDate(communicationDto.getRecordDate());
							communication.setProjName(communicationDto.getProjName());
							communication.setProjStoreName(communicationDto.getProjStoreName());
							communicationBizImpl.save(communication);
						}
					}
					CommunicationActivity.handler.sendMessage(CommunicationActivity.handler.obtainMessage(0xCD, communicationResult));
				}else{
					CommunicationActivity.handler.sendMessage(CommunicationActivity.handler.obtainMessage(0xDE, communicationResult));
				}
				
			}else{
				Message msg = new Message();
				msg.what=0xDD;
				Bundle bundle = new Bundle();
				bundle.putSerializable("msginfo", communicationResult.getMsg());
				msg.setData(bundle);
				CommunicationActivity.handler.sendMessage(msg);
			}
		}
		
	}

	@Override
	public String getCommandID() {
		
		return Protocol.getCommunication;
	}

	@Override
	public Class<?> getCommandClass() {
		// TODO Auto-generated method stub
		return CommunicationResult.class;
	}

}
